<template>
  <div class="home">
    <div class="homeWarp">
      <div class="nav">
        <div class="navCate">
          <span class="navCateTitle">商品目录</span>
          <ul class="navParent">
            <li class="cate" v-for="cate1 in homeData.category" :key="cate1.catId" @click="onCateSearchClick(cate1)">
              <span class="cateTitle">
                {{ cate1.catName }}
                <el-icon class="icon"><ArrowRight /></el-icon>
              </span>
              <!-- <ul class="cate2" v-if="cate1.categoryChilden?.length > 0">
                <li class="cate2Item" v-for="cate2 in cate1.categoryChilden" :key="cate2.catId">
                  <div class="label">
                    <span class="text">{{ cate2.catName }} </span>
                    <el-icon class="icon"><ArrowRight /></el-icon>
                  </div>
                  <ul v-if="cate2.categoryChilden?.length > 0" class="cate3">
                    <li v-for="cate3 in cate2.categoryChilden" :key="cate3.catId" @click="onToSearchClick(cate2.catId, cate3.catId)">
                      <span> {{ cate3.catName }} </span>
                    </li>
                  </ul>
                </li>
              </ul> -->
            </li>
          </ul>
        </div>
      </div>
      <div>
        <div class="bannerimgWarp">
          <el-carousel v-if="homeData.banner.length > 0" class="bannerBox">
            <el-carousel-item class="bannerCon" v-for="item in homeData.banner" :key="item.floorInfoImg">
              <el-image :src="item.floorInfoImg" fit="cover" @click="onClickSkip(item.floorInfoUrl, item.floorInfoVal, item.floorInfoName)"></el-image>
            </el-carousel-item>
          </el-carousel>
        </div>
        <div class="recomGoodsWarp">
          <div class="recomTittle">
            <span>商品推荐</span>
            <img class="goodsSale" src="@/assets/images/cuxiao.svg" />
            <img class="more-icon" src="@/assets/images/more.svg" @click="onToGoodsListClick('')" />
          </div>
          <ul class="goods">
            <li v-for="goods in homeData.goodsList" :key="goods.goodsId">
              <div @click="onGoodsClick(goods)">
                <img :src="goods.floorInfoImg" class="goodsImg" />
                <div class="goodsinfo">
                  <p class="goods-sub">{{ goods.goodsName }}</p>
                  <p class="goods-price"><span>￥</span>{{ goods.price }}</p>
                </div>
              </div>
            </li>
          </ul>
        </div>
        <div class="zhongyaoGoodsWarp">
          <div class="zhongyaoTittle">
            <span>皮肤修复辅料</span>
            <img class="more-icon" src="@/assets/images/more.svg" @click="onToGoodsListClick('')" />
            <!-- <img class="more-icon" src="@/assets/images/more.svg" @click="onToGoodsListClick('INSTRUMENT')" /> -->
          </div>
          <ul class="goods">
            <li v-for="goods in homeData.insgoods" :key="goods" @click="onClickSkip(goods.floorInfoUrl, goods.floorInfoVal, goods.floorInfoName)">
              <img :src="goods.floorInfoImg" />
            </li>
          </ul>
        </div>
        <div class="zhongyaoGoodsWarp">
          <div class="zhongyaoTittle">
            <span>一次性耗材用品</span>
            <img class="more-icon" src="@/assets/images/more.svg" @click="onToGoodsListClick('')" />
            <!-- <img class="more-icon" src="@/assets/images/more.svg" @click="onToGoodsListClick('HCONSUMABLES')" /> -->
          </div>
          <ul class="goods">
            <li v-for="goods in homeData.Hmgoods" :key="goods" @click="onClickSkip(goods.floorInfoUrl, goods.floorInfoVal, goods.floorInfoName)">
              <img :src="goods.floorInfoImg" />
            </li>
          </ul>
        </div>
        <div class="zhongyaoGoodsWarp">
          <div class="zhongyaoTittle">
            <span>医用采血管</span>
            <img class="more-icon" src="@/assets/images/more.svg" @click="onToGoodsListClick('')" />
            <!-- <img class="more-icon" src="@/assets/images/more.svg" @click="onToGoodsListClick('LCONSUMABLES')" /> -->
          </div>
          <ul class="goods">
            <li v-for="goods in homeData.Lmgoods" :key="goods" @click="onClickSkip(goods.floorInfoUrl, goods.floorInfoVal, goods.floorInfoName)">
              <img :src="goods.floorInfoImg" />
            </li>
          </ul>
        </div>
        <!-- <div class="zhongyaoGoodsWarp">
          <div class="zhongyaoTittle">
            <span>试剂商品</span>
            <img class="more-icon" src="@/assets/images/more.svg" @click="onToGoodsListClick('REAGENT')" />
          </div>
          <ul class="goods">
            <li v-for="goods in homeData.Reagoods" :key="goods" @click="onClickSkip(goods.floorInfoUrl, goods.floorInfoVal, goods.floorInfoName)">
              <img :src="goods.floorInfoImg" />
            </li>
          </ul>
        </div>
        <div class="zhongyaoGoodsWarp">
          <div class="zhongyaoTittle">
            <span>常规商品</span>
            <img class="more-icon" src="@/assets/images/more.svg" @click="onToGoodsListClick('CONVENTION')" />
          </div>
          <ul class="goods">
            <li v-for="goods in homeData.Congoods" :key="goods" @click="onClickSkip(goods.floorInfoUrl, goods.floorInfoVal, goods.floorInfoName)">
              <img :src="goods.floorInfoImg" />
            </li>
          </ul>
        </div> -->
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, onMounted, ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ArrowRight } from '@element-plus/icons-vue'
import api from '@/api'
import Cart from '@/components/cart/index.vue'

const router = useRouter()
const route = useRoute()

const homeData = reactive({
  banner: [],
  category: [],
  goodsList: [],
  insgoods: [],
  Hmgoods: [],
  Lmgoods: [],
  Reagoods: [],
  Congoods: [],
  list: [],
  total: 0,
  search: {
    page: 1,
    limit: 20
  }
})

let cartVisible = ref(false)
let shoopingCartRef = ref(null)

let keyword = route.query.keyword || ''

const goodsCateData = async () => {
  let { data, err } = await api.get('/medinstrument/site/getAllGoodsCategory')
  if (err) {
    ElMessage.error(err)
  } else {
    homeData.category = data.result?.filter((e) => e.categoryChilden)?.filter((f) => f.categoryChilden)
  }
}

const loadData = async () => {
  let { data, err } = await api.get('/medinstrument/site/index')
  if (err) {
    ElMessage.error(err)
  } else {
    homeData.banner = data.banner
    homeData.goodsList = data.goodsrefer?.map((item) => {
      let { floorInfoVal } = item
      floorInfoVal = JSON.parse(floorInfoVal)
      return {
        ...item,
        ...floorInfoVal[0]
      }
    })

    homeData.insgoods = data.insgoods?.map((item) => item)
    homeData.Hmgoods = data.Hmgoods?.map((item) => item)
    homeData.Lmgoods = data.Lmgoods?.map((item) => item)
    homeData.Reagoods = data.Reagoods?.map((item) => item)
    homeData.Congoods = data.Congoods?.map((item) => item)
    // homeData.cat = data.cat?.map((item) => item)
  }

  if (keyword) {
    let url = `/medinstrument/site/search`
    let { data, err } = await api.post(url, { keyword, pageNum: homeData.search.page, pageSize: homeData.search.limit })
    if (err) {
      ElMessage.error(err)
    } else {
      homeData.list = data.list
    }
  }
}

const onToGoodsListClick = (type) => {
  router.push({ path: '/goodsList', query: { goodsType: type } })
}

const onClickSkip = (url, id, name) => {
  if (url === '跳转店铺') {
    let { href } = router.resolve({
      path: '/store',
      query: { supplierId: id, supplierName: name }
    })
    window.open(href, '_blank')
  } else {
    if (typeof id === 'string') {
      try {
        const obj = JSON.parse(id)
        if (typeof obj === 'object' && obj) {
          let goodsId = obj.map((e) => e.goodsId).join(',')
          let { href } = router.resolve({
            path: `/goods/${goodsId}`
          })
          window.open(href, '_blank')
        } else {
          let { href } = router.resolve({
            path: `/goods/${id}`
          })
          window.open(href, '_blank')
        }
      } catch (e) {}
    }
  }
}

const onGoodsClick = (goods) => {
  let { href } = router.resolve({
    path: `/goods/${goods.goodsId}`
  })
  window.open(href, '_blank')
}

const onToSearchClick = (goodsType, goodsCatIdFirst, goodsCatIdSecond) => {
  let { href } = router.resolve({
    path: '/goodsList',
    query: { goodsCatIdFirst, goodsCatIdSecond, goodsType }
  })

  window.open(href, '_blank')
}

const onCateSearchClick = (cate1) => {
  let { href } = router.resolve({
    path: '/goodsList',
    query: { goodsCatIdFirst: cate1.catId, goodsCatIdSecond: '', goodsType: '' }
  })

  window.open(href, '_blank')
}

const showMessage = () => {
  ElMessageBox.confirm('您需要具备相关资质才能购买商品，是否立即申请资质？', '提示', {
    cancelButtonText: '取消',
    confirmButtonText: '申请',
    cancelButtonClass: 'cancelButtonClass',
    confirmButtonClass: 'confirmButtonClass',
    customClass: 'message-box',
    showClose: false
  })
    .then(() => {
      let { href } = router.resolve({ path: '/user' })
      window.open(href, '_blank')
    })
    .catch(() => {})
}

onMounted(() => {
  if (history.state.params === 'YES') {
    showMessage()
  }
  loadData()
  goodsCateData()

  document.title = '首页'
})
</script>

<style lang="scss" scoped>
.home {
  background: #fff;

  .homeWarp {
    width: 1080px;
    padding: 0 20px;
    margin: 0 auto;
    box-sizing: border-box;
    position: relative;

    li {
      cursor: pointer;
    }

    .nav {
      position: absolute;
      top: -44px;
      left: 20px;

      .navCate {
        display: inline-block;
        position: relative;

        .navCateTitle {
          display: block;
          width: 240px;
          height: 42px;
          line-height: 42px;
          font-size: 16px;
          font-weight: 500;
          color: #fff;
          background-color: #64c8b9;
          text-align: center;
          cursor: pointer;
        }

        &:hover {
          .navParent {
            display: block;
          }
        }

        .cateTitle {
          display: flex;
          align-items: center;
          justify-content: space-between;
          box-sizing: border-box;
          cursor: pointer;
        }

        .navParent {
          display: none;
          position: relative;
          width: 240px;
          height: 300px;
          overflow-y: auto;
          background-color: #3e424c;
          left: 0;
          z-index: 100;
          &::after {
            content: '';
            position: absolute;
            background: #3e424c;
            width: 240px;
            height: 329px;
            top: 0;
            left: 0;
            z-index: -1;
          }
        }

        .cate {
          color: #fff;
          padding: 10px 15px;

          &:hover {
            background-color: #64c8b9;
            .cate2 {
              position: absolute;
              left: 240px;
              top: 0;
              display: block;
              color: #64c8b9;
            }

            .cate3 {
              border-bottom: 1px dashed rgba(220, 223, 230, 1);
            }
          }
          // .cate2 {
          //   background-color: #fff;
          //   display: none;
          //   min-height: 329px;
          //   width: 800px;
          //   cursor: pointer;
          //   box-sizing: border-box;

          //   .cate2Item {
          //     margin: 10px;
          //     font-size: 16px;
          //     display: flex;
          //     align-items: flex-start;

          //     .label {
          //       display: flex;
          //       align-items: center;
          //       justify-content: space-around;
          //       .text,
          //       .icon {
          //         color: var(--color-text);
          //       }

          //       .text {
          //         width: 150px;
          //       }
          //     }
          //   }
          // }

          // .cate3 {
          //   flex: 1;
          //   padding-bottom: 10px;

          //   li {
          //     display: inline-block;
          //     margin: 0 10px 5px 10px;
          //     color: var(--color-text);
          //     cursor: pointer;
          //     &:hover {
          //       color: #64c8b9;
          //     }
          //   }
          // }
        }
      }
    }
    .bannerimgWarp {
      height: 329px;
      margin: 0 auto;
      overflow: hidden;

      .bannerBox {
        cursor: pointer;
      }
    }

    .recomGoodsWarp,
    .zhongyaoGoodsWarp {
      .recomTittle,
      .zhongyaoTittle {
        margin-top: 20px;
      }
      span {
        font-size: 24px;
        line-height: 32px;
        color: var(--color-text);
        font-weight: 700;
      }

      .more-icon {
        cursor: pointer;
      }
    }

    .recomGoodsWarp {
      .recomTittle {
        display: flex;
        align-items: center;
        height: 32px;
        margin-bottom: 17px;

        .goodsSale {
          margin: 0 10px 0 5px;
        }
      }
      .goods {
        display: flex;

        li {
          box-sizing: border-box;
          padding: 10px;
          border-radius: 10px;
          overflow: hidden;
          margin-right: 20px;
          .goodsImg {
            width: 176px;
            height: 176px;
            border-radius: 10px;
          }

          &:hover {
            box-shadow: 0px 3px 6px 0px rgba(0, 0, 0, 0.15);
            .goodsinfo .goods-sub {
              color: #e86600;
            }
          }
          .goodsinfo {
            margin-top: 10px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            box-sizing: border-box;
            .goods-sub {
              width: 166px;
              height: 42px;
              line-height: 42px;
              font-size: 16px;
              color: var(--color-text);
              text-align: justify;
              overflow: hidden;
              white-space: nowrap;
              text-overflow: ellipsis;
              margin-bottom: 5px;
            }
            .goods-price {
              width: 47px;
              height: 27px;
              line-height: 27px;
              font-size: 20px;
              color: var(--color-primary);
              font-weight: 600;

              span {
                width: 7px;
                height: 27px;
                line-height: 27px;
                font-size: 12px;
                color: var(--color-primary);
              }
            }
          }
        }

        li:last-child {
          margin-right: 0;
        }
      }
    }
    .zhongyaoGoodsWarp {
      .zhongyaoTittle {
        display: flex;
        align-items: center;
        margin-bottom: 10px;
        span {
          margin-right: 10px;
        }
      }
      .goods {
        display: flex;

        li {
          width: 248px;
          height: 300px;
          border-radius: 10px;
          margin-right: 16px;

          img {
            width: 100%;
            height: 100%;
            border-radius: 10px;
          }
        }
      }
    }

    .zhongyaoGoodsWarp:last-child {
      margin-bottom: 117px;
    }
  }
}
</style>
